package dfd.diagram.providers;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

import dfd.DfdPackage;
import dfd.diagram.edit.parts.ActionEditPart;
import dfd.diagram.edit.parts.AndEditPart;
import dfd.diagram.edit.parts.DfdEditPart;
import dfd.diagram.edit.parts.EndEditPart;
import dfd.diagram.edit.parts.OrEditPart;
import dfd.diagram.edit.parts.ProcessEditPart;
import dfd.diagram.edit.parts.StartEditPart;
import dfd.diagram.edit.parts.StorageEditPart;
import dfd.diagram.edit.parts.TransitionEditPart;
import dfd.diagram.edit.parts.XorEditPart;
import dfd.diagram.part.DfdDiagramEditorPlugin;

/**
 * @generated
 */
public class DfdElementTypes {

	/**
	 * @generated
	 */
	private DfdElementTypes() {
	}

	/**
	 * @generated
	 */
	private static Map<IElementType, ENamedElement> elements;

	/**
	 * @generated
	 */
	private static ImageRegistry imageRegistry;

	/**
	 * @generated
	 */
	private static Set<IElementType> KNOWN_ELEMENT_TYPES;

	/**
	 * @generated
	 */
	public static final IElementType Dfd_1000 = getElementType("DFD.diagram.Dfd_1000"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType And_2001 = getElementType("DFD.diagram.And_2001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Process_2002 = getElementType("DFD.diagram.Process_2002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Xor_2003 = getElementType("DFD.diagram.Xor_2003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Start_2004 = getElementType("DFD.diagram.Start_2004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Or_2005 = getElementType("DFD.diagram.Or_2005"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType End_2006 = getElementType("DFD.diagram.End_2006"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Storage_2007 = getElementType("DFD.diagram.Storage_2007"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Action_3001 = getElementType("DFD.diagram.Action_3001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Transition_4001 = getElementType("DFD.diagram.Transition_4001"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
		}
		return imageRegistry;
	}

	/**
	 * @generated
	 */
	private static String getImageRegistryKey(ENamedElement element) {
		return element.getName();
	}

	/**
	 * @generated
	 */
	private static ImageDescriptor getProvidedImageDescriptor(
			ENamedElement element) {
		if (element instanceof EStructuralFeature) {
			EStructuralFeature feature = ((EStructuralFeature) element);
			EClass eContainingClass = feature.getEContainingClass();
			EClassifier eType = feature.getEType();
			if (eContainingClass != null && !eContainingClass.isAbstract()) {
				element = eContainingClass;
			} else if (eType instanceof EClass
					&& !((EClass) eType).isAbstract()) {
				element = eType;
			}
		}
		if (element instanceof EClass) {
			EClass eClass = (EClass) element;
			if (!eClass.isAbstract()) {
				return DfdDiagramEditorPlugin.getInstance()
						.getItemImageDescriptor(
								eClass.getEPackage().getEFactoryInstance()
										.create(eClass));
			}
		}
		// TODO : support structural features
		return null;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(ENamedElement element) {
		String key = getImageRegistryKey(element);
		ImageDescriptor imageDescriptor = getImageRegistry().getDescriptor(key);
		if (imageDescriptor == null) {
			imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
		}
		return imageDescriptor;
	}

	/**
	 * @generated
	 */
	public static Image getImage(ENamedElement element) {
		String key = getImageRegistryKey(element);
		Image image = getImageRegistry().get(key);
		if (image == null) {
			ImageDescriptor imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
			image = getImageRegistry().get(key);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImageDescriptor(element);
	}

	/**
	 * @generated
	 */
	public static Image getImage(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImage(element);
	}

	/**
	 * Returns 'type' of the ecore object associated with the hint.
	 * 
	 * @generated
	 */
	public static ENamedElement getElement(IAdaptable hint) {
		Object type = hint.getAdapter(IElementType.class);
		if (elements == null) {
			elements = new IdentityHashMap<IElementType, ENamedElement>();

			elements.put(Dfd_1000, DfdPackage.eINSTANCE.getDfd());

			elements.put(And_2001, DfdPackage.eINSTANCE.getAnd());

			elements.put(Process_2002, DfdPackage.eINSTANCE.getProcess());

			elements.put(Xor_2003, DfdPackage.eINSTANCE.getXor());

			elements.put(Start_2004, DfdPackage.eINSTANCE.getStart());

			elements.put(Or_2005, DfdPackage.eINSTANCE.getOr());

			elements.put(End_2006, DfdPackage.eINSTANCE.getEnd());

			elements.put(Storage_2007, DfdPackage.eINSTANCE.getStorage());

			elements.put(Action_3001, DfdPackage.eINSTANCE.getAction());

			elements.put(Transition_4001, DfdPackage.eINSTANCE.getTransition());
		}
		return (ENamedElement) elements.get(type);
	}

	/**
	 * @generated
	 */
	private static IElementType getElementType(String id) {
		return ElementTypeRegistry.getInstance().getType(id);
	}

	/**
	 * @generated
	 */
	public static boolean isKnownElementType(IElementType elementType) {
		if (KNOWN_ELEMENT_TYPES == null) {
			KNOWN_ELEMENT_TYPES = new HashSet<IElementType>();
			KNOWN_ELEMENT_TYPES.add(Dfd_1000);
			KNOWN_ELEMENT_TYPES.add(And_2001);
			KNOWN_ELEMENT_TYPES.add(Process_2002);
			KNOWN_ELEMENT_TYPES.add(Xor_2003);
			KNOWN_ELEMENT_TYPES.add(Start_2004);
			KNOWN_ELEMENT_TYPES.add(Or_2005);
			KNOWN_ELEMENT_TYPES.add(End_2006);
			KNOWN_ELEMENT_TYPES.add(Storage_2007);
			KNOWN_ELEMENT_TYPES.add(Action_3001);
			KNOWN_ELEMENT_TYPES.add(Transition_4001);
		}
		return KNOWN_ELEMENT_TYPES.contains(elementType);
	}

	/**
	 * @generated
	 */
	public static IElementType getElementType(int visualID) {
		switch (visualID) {
		case DfdEditPart.VISUAL_ID:
			return Dfd_1000;
		case AndEditPart.VISUAL_ID:
			return And_2001;
		case ProcessEditPart.VISUAL_ID:
			return Process_2002;
		case XorEditPart.VISUAL_ID:
			return Xor_2003;
		case StartEditPart.VISUAL_ID:
			return Start_2004;
		case OrEditPart.VISUAL_ID:
			return Or_2005;
		case EndEditPart.VISUAL_ID:
			return End_2006;
		case StorageEditPart.VISUAL_ID:
			return Storage_2007;
		case ActionEditPart.VISUAL_ID:
			return Action_3001;
		case TransitionEditPart.VISUAL_ID:
			return Transition_4001;
		}
		return null;
	}

}
